<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  
  
    <meta name="keywords" content="lnsix">
  
  
    <meta name="description" content="我的个人博客！">
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    nginx配置文件 |
    
    lnsix的个人博客</title>
  
    <link rel="shortcut icon" href="/favicon.ico">
  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<script src="/js/pace.min.js"></script>

<meta name="generator" content="Hexo 5.2.0"></head>

<body>
<main class="content">
  <section class="outer">
  <article id="drafts-nginx配置文件" class="article article-type-drafts" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      nginx配置文件
    </h1>
  
  




    </header>
    

    
    <div class="article-meta">
      <a href="/2020/11/19/nginx%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/" class="article-date">
  <time datetime="2020-11-18T16:00:00.000Z" itemprop="datePublished">2020-11-19</time>
</a>
      
    </div>
    

    
    
<div class="tocbot"></div>

    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="nginx常用配置文件"><a href="#nginx常用配置文件" class="headerlink" title="nginx常用配置文件"></a>nginx常用配置文件</h1><h2 id="1-主配置文件"><a href="#1-主配置文件" class="headerlink" title=" 1.主配置文件"></a> 1.主配置文件</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">user www www;</span><br><span class="line">worker_processes  2;</span><br><span class="line">error_log logs&#x2F;error.log;</span><br><span class="line">pid logs&#x2F;nginx.pid;</span><br><span class="line">worker_rlimit_nofile 51200;</span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">        use epoll;</span><br><span class="line">        worker_connections  51200;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line">        charset utf-8;</span><br><span class="line">        include       mime.types;</span><br><span class="line">        default_type  application&#x2F;octet-stream;</span><br><span class="line">        sendfile  on;</span><br><span class="line">        tcp_nopush on;</span><br><span class="line">        tcp_nodelay on;</span><br><span class="line">        keepalive_timeout  60;</span><br><span class="line">        server_tokens off;</span><br><span class="line">        </span><br><span class="line"> </span><br><span class="line">        limit_conn_log_level error;  #当达到最大限制连接数时，记录日志的等级</span><br><span class="line">        limit_conn_status 503;       #该参数在1.3.15版本引入的。指定当超过限制时，返回的状态码（默认是503），code值只能设置在400到599之间。</span><br><span class="line">        limit_conn_zone $binary_remote_addr zone&#x3D;one:10m;</span><br><span class="line">        #用于定义一个zone，该zome将会被用于存储会话状态。能够存储的会话数量是由定义的size大小和系统参数memory_max_size的大小决定的。</span><br><span class="line">        #zone是共享内存空间，作用是保存每个key对应的连接数，此处可以按照需求自己定义名字。</span><br><span class="line">        limit_conn_zone $server_name zone&#x3D;perserver:10m; #</span><br><span class="line">        limit_req_zone $binary_remote_addr zone&#x3D;allips:10m   rate&#x3D;10r&#x2F;s;#对每个连接的速率限制。参数rate的单位是字节&#x2F;秒，设置为0将关闭限速。 </span><br><span class="line">        #按连接限速而不是按IP限制，因此如果某个客户端同时开启了两个连接，那么客户端的整体速率是这条指令设置值的2倍。</span><br><span class="line"></span><br><span class="line">        log_format main &#39;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#39;</span><br><span class="line">        &#39;$status $body_bytes_sent &quot;$http_referer&quot; &#39;</span><br><span class="line">        &#39;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot; &quot;$request_time&quot;&#39;;</span><br><span class="line">        access_log logs&#x2F;access.log main;</span><br><span class="line"></span><br><span class="line">        gzip on;</span><br><span class="line">        gzip_min_length 1k;</span><br><span class="line">        gzip_buffers 4 16k;</span><br><span class="line">        gzip_http_version 1.1;</span><br><span class="line">        gzip_comp_level 2;</span><br><span class="line">        gzip_types text&#x2F;plain application&#x2F;x-javascript text&#x2F;css application&#x2F;xml;</span><br><span class="line">        gzip_vary on;</span><br><span class="line"></span><br><span class="line">        include vhosts&#x2F;*.conf;</span><br><span class="line">        include upstream&#x2F;*.conf;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-主机配置文件"><a href="#2-主机配置文件" class="headerlink" title=" 2.主机配置文件"></a> 2.主机配置文件</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">        listen  80;</span><br><span class="line">        server_name xxx.xxx.xxx;</span><br><span class="line">        root &#x2F;data&#x2F;web&#x2F;myblog;</span><br><span class="line">        index index.html index.php;</span><br><span class="line">        access_log  &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;logs&#x2F;blog&#x2F;access.log main;</span><br><span class="line">        error_log  &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;logs&#x2F;blog&#x2F;error.log;</span><br><span class="line"></span><br><span class="line">        limit_conn  one  10;</span><br><span class="line">        limit_conn perserver 1000;</span><br><span class="line">        limit_req zone&#x3D;allips  burst&#x3D;10 nodelay;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        location ~ \.php$ &#123;</span><br><span class="line">            root           html;</span><br><span class="line">            fastcgi_pass   127.0.0.1:9000;</span><br><span class="line">            fastcgi_index  index.php;</span><br><span class="line">            fastcgi_param  SCRIPT_FILENAME  &#x2F;scripts$fastcgi_script_name;</span><br><span class="line">            include        fastcgi_params;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        error_page  500 502 503 504  &#x2F;50x.html;</span><br><span class="line">        location &#x3D; &#x2F;50x.html &#123;</span><br><span class="line">              root   &#x2F;opt&#x2F;web&#x2F;error_page&#x2F;;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-定制json格式的日志文件，记录客户端IP"><a href="#3-定制json格式的日志文件，记录客户端IP" class="headerlink" title=" 3.定制json格式的日志文件，记录客户端IP"></a> 3.定制json格式的日志文件，记录客户端IP</h2><ul>
<li>修改主配置文件中日志格式 </li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">log_format  main escape&#x3D;json &#39;&#123;&quot;remote_addr&quot;:&quot;$remote_addr&quot;,&#39;</span><br><span class="line">              &#39;&quot;remote_user&quot;:&quot;$remote_user&quot;,&#39;</span><br><span class="line">              &#39;&quot;time_local&quot;:&quot;$time_local&quot;,&#39;</span><br><span class="line">              &#39;&quot;request&quot;:&quot;$request&quot;,&#39;</span><br><span class="line">              &#39;&quot;status&quot;:&quot;$status&quot;,&#39;</span><br><span class="line">              &#39;&quot;body_bytes_sent&quot;:&quot;$body_bytes_sent&quot;,&#39;</span><br><span class="line">              &#39;&quot;http_referer&quot;:&quot;$http_referer&quot;,&#39;</span><br><span class="line">              &#39;&quot;http_user_agent&quot;:&quot;$http_user_agent&quot;,&#39;</span><br><span class="line">              &#39;&quot;http_x_forwarded_for&quot;:&quot;$http_x_forwarded_for&quot;&#125;&#39;;</span><br><span class="line">access_log logs&#x2F;access.log main;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/11/19/nginx%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/" data-id="ckhoo4y440000p4vja67v3jfz" class="article-share-link">
        分享
      </a>
      
    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/11/19/Centos7%E4%B8%8A%E4%BD%BF%E7%94%A8shell%E8%84%9A%E6%9C%AC%E4%B8%80%E9%94%AE%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2lnmp/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            Centos7上使用shell脚本一键安装部署lnmp
          
        </div>
      </a>
    
    
      <a href="/2020/11/18/my-first-article/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">my_first_article</div>
      </a>
    
  </nav>


  

  
  
  
  

</article>
</section>
  <footer class="footer">
  <div class="outer">
    <div class="float-right">
      <ul class="list-inline">
  
    <li><i class="fe fe-smile-alt"></i> <span id="busuanzi_value_site_uv"></span></li>
  
    <li><i class="fe fe-bookmark"></i> <span id="busuanzi_value_page_pv"></span></li>
  
</ul>
    </div>
    <ul class="list-inline">
      <li>&copy; 2020 lnsix的个人博客</li>
      <li>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></li>
      <li>Theme  <a target="_blank" rel="noopener" href="https://github.com/zhwangart/hexo-theme-ocean">Ocean</a></li>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>
<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="lnsix的个人博客"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">Home</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">Archives</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">Gallery</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">About</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="搜索">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>

<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


  
<script src="/fancybox/jquery.fancybox.min.js"></script>




  
<script src="/js/tocbot.min.js"></script>

  <script>
    // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
    tocbot.init({
      tocSelector: '.tocbot',
      contentSelector: '.article-entry',
      headingSelector: 'h1, h2, h3, h4, h5, h6',
      hasInnerContainers: true,
      scrollSmooth: true,
      positionFixedSelector: '.tocbot',
      positionFixedClass: 'is-position-fixed',
      fixedSidebarOffset: 'auto',
    });
  </script>



<script src="/js/ocean.js"></script>


</body>
</html>